package feng.xml;

import java.io.File;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * XML文件解析操作类。采用Dom4J实现
 * @author 冯昌硕
 * @create 2012-10-22 下午11:08:37
 */
public class XmlFile {

    private static final Logger log = Logger.getLogger(XmlFile.class);
    
    /**
     * XML文件路径
     */
    private String xmlFilePath;
    
    /**
     * XML解析的Dom对象
     */
    private Document document;
    
    /**
     * XML的根结点
     */
    private Element rootElement;
    
    public XmlFile(String xmlFilePath) {
        this.xmlFilePath = xmlFilePath;
    }
    
    /**
     * 功能：解析XML文件
     * @create 2012-10-22 下午11:12:39
     */
    public boolean parseXmlFile() {
        SAXReader saxReader = new SAXReader();
        try {
            File xmlFile = new File(xmlFilePath);
            if(!xmlFile.exists() || xmlFile.isDirectory()) {
                log.info(xmlFilePath + "不存在或者是目录,无法解析");
                return false;
            }
            
            document = saxReader.read(xmlFile);
            
            rootElement = document.getRootElement();
            
            return true;
        } catch (DocumentException e) {
            log.error("文件" + xmlFilePath + "解析异常:" + e.toString());
            e.printStackTrace();
            return false;
        }
    }

    public Element getRootElement() {
        return rootElement;
    }

    public void setRootElement(Element rootElement) {
        this.rootElement = rootElement;
    }
    
}
